from fastapi import APIRouter
from typing import List, Dict, Any
from pydantic import BaseModel, Field
from datetime import date
from apps.services.dashboard import (
    get_dashboard_stats, get_growth_data, get_student_trend,
    get_coach_specialization, get_appointment_status, get_student_gender,
    get_student_license_type, get_coach_rating, get_student_progress,
    get_coach_active, get_program_active, get_training_status,
    get_recent_students, get_recent_appointments
)

dashboard = APIRouter()


@dashboard.get("/stats")
async def get_dashboard_stats_api():
    """获取统计数据"""
    try:
        stats_data = await get_dashboard_stats()
        return {"status": 200, "message": "success", "data": stats_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving dashboard stats: {str(e)}"}


@dashboard.get("/growth")
async def get_growth_data_api():
    """获取增长数据"""
    try:
        growth_data = await get_growth_data()
        return {"status": 200, "message": "success", "data": growth_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving growth data: {str(e)}"}


@dashboard.get("/student-trend")
async def get_student_trend_api():
    """获取学员增长趋势"""
    try:
        trend_data = await get_student_trend()
        return {"status": 200, "message": "success", "data": trend_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving student trend: {str(e)}"}


@dashboard.get("/coach-specialization")
async def get_coach_specialization_api():
    """获取教练技能分布"""
    try:
        specialization_data = await get_coach_specialization()
        return {"status": 200, "message": "success", "data": specialization_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving coach specialization: {str(e)}"}


@dashboard.get("/appointment-status")
async def get_appointment_status_api():
    """获取预约状态分布"""
    try:
        status_data = await get_appointment_status()
        return {"status": 200, "message": "success", "data": status_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving appointment status: {str(e)}"}


@dashboard.get("/student-gender")
async def get_student_gender_api():
    """获取学员性别分布"""
    try:
        gender_data = await get_student_gender()
        return {"status": 200, "message": "success", "data": gender_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving student gender: {str(e)}"}


@dashboard.get("/student-license-type")
async def get_student_license_type_api():
    """获取学员驾照类型分布"""
    try:
        license_data = await get_student_license_type()
        return {"status": 200, "message": "success", "data": license_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving student license type: {str(e)}"}


@dashboard.get("/coach-rating")
async def get_coach_rating_api():
    """获取教练评分分布"""
    try:
        rating_data = await get_coach_rating()
        return {"status": 200, "message": "success", "data": rating_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving coach rating: {str(e)}"}


@dashboard.get("/student-progress")
async def get_student_progress_api():
    """获取学员学习进度分布"""
    try:
        progress_data = await get_student_progress()
        return {"status": 200, "message": "success", "data": progress_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving student progress: {str(e)}"}


@dashboard.get("/coach-active")
async def get_coach_active_api():
    """获取教练活跃状态"""
    try:
        active_data = await get_coach_active()
        return {"status": 200, "message": "success", "data": active_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving coach active status: {str(e)}"}


@dashboard.get("/program-active")
async def get_program_active_api():
    """获取训练项目活跃状态"""
    try:
        active_data = await get_program_active()
        return {"status": 200, "message": "success", "data": active_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving program active status: {str(e)}"}


@dashboard.get("/training-status")
async def get_training_status_api():
    """获取学员训练状态分布"""
    try:
        status_data = await get_training_status()
        return {"status": 200, "message": "success", "data": status_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving training status: {str(e)}"}


class RecentStudentResponse(BaseModel):
    fullname: str = Field(..., description="学员姓名")
    enrollment_date: date = Field(..., description="入学日期")
    status: str = Field(..., description="学员状态")


class RecentStudentListResponse(BaseModel):
    data: List[RecentStudentResponse] = Field(..., description="最新学员列表")


@dashboard.get("/recent-students", response_model=RecentStudentListResponse)
async def get_recent_students_api():
    """获取最新学员"""
    try:
        students_data = await get_recent_students()
        return {"status": 200, "message": "success", "data": students_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving recent students: {str(e)}"}


@dashboard.get("/recent-appointments")
async def get_recent_appointments_api():
    """获取最新预约"""
    try:
        appointments_data = await get_recent_appointments()
        return {"status": 200, "message": "success", "data": appointments_data}
    except Exception as e:
        return {"status": 500, "message": f"Error retrieving recent appointments: {str(e)}"}
